package com.climber.spb_rabbitmq.manualack;

import com.climber.spb_rabbitmq.utils.RabbitMQUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class MConsumer1 {
    private static final String TASK_QUEUE_NAME="ack_queue_durable";

    public static void main(String[] args) {
        try {
            Channel channel = RabbitMQUtils.getChannel();
            channel.basicQos(2);
            System.out.println("MConsumer1 *********");
            DeliverCallback deliverCallback=(consumerTag,message)->{
                String s = new String(message.getBody(), StandardCharsets.UTF_8);
                System.out.println("MConsumer1 received message : "+s);
                try {
                    Thread.sleep(1000*2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("finished");
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            };
            CancelCallback cancelCallback=(consumerTag)->{
                System.out.println(consumerTag);
                System.out.println("MConsumer1 receive interrupted");
            };
            channel.basicConsume(TASK_QUEUE_NAME,false,deliverCallback,cancelCallback);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}
